<?
$sub_menu = "400300";
include_once("./_common.php");
include_once("$g4[path]/lib/shop.lib.php");

if($Mode == "STATUS")
{
	auth_check($auth[$sub_menu], "w");
	for($i=0;$i<count($sidx);$i++)
	{
		$order_row = mysql_fetch_array(mysql_query("select * from g4_order where idx='".$sidx[$i]."' limit 1"));

		$tmp_status_history = explode("/",$order_row[status_history]);

		if($list_status != $order_row[status])
		{

			if($list_status == "6")//취소
			{
				if($order_row[mb_part]=="member")
				{
					if($order_row[pay_point] > 0)
					{
						$point_sql = "insert into g4_point set ";
						$point_sql.= "mb_id = '".$order_row[mb_id]."', ";
						$point_sql.= "po_datetime = now(), ";
						$point_sql.= "po_content = '주문번호 [".$order_row[oid]."]건 결제 취소', ";
						$point_sql.= "po_point = '".$order_row[pay_point]."', ";
						$point_sql.= "po_rel_table = '@shop', ";
						$point_sql.= "po_rel_id = 'system', ";
						$point_sql.= "po_rel_action = '".date("Y-m-d")."' ";

						$point_rst = mysql_query($point_sql);

						if($point_rst)
						{
							$member_sql = "update g4_member set mb_point = mb_point + ".$order_row[pay_point]." where mb_id = '".$order_row[mb_id]."' limit 1";
							$member_rst = mysql_query($member_sql);
						}
					}
				}

				$order_goods_sql = "select * from g4_order_goods where oid = '".$order_row[oid]."' ";
				$order_goods_rst = mysql_query($order_goods_sql);
				while($order_goods_row = mysql_fetch_array($order_goods_rst))
				{
					$goods_update_sql = "update g4_goods set g_cnt = g_cnt + ".$order_goods_row[cnt].", g_sale = g_sale - ".$order_goods_row[cnt]." where g_code='".$order_goods_row[g_code]."' limit 1";

					$goods_update_rst = mysql_query($goods_update_sql);
				}
			}

			if($order_row[status] == "6")
			{
				if($order_row[mb_part]=="member")
				{
					if($order_row[pay_point] > 0)
					{
						$point_sql = "insert into g4_point set ";
						$point_sql.= "mb_id = '".$order_row[mb_id]."', ";
						$point_sql.= "po_datetime = now(), ";
						$point_sql.= "po_content = '주문번호 [".$order_row[oid]."]건 결제취소에서 일반상태로 변경', ";
						$point_sql.= "po_point = '-".$order_row[pay_point]."', ";
						$point_sql.= "po_rel_table = '@shop', ";
						$point_sql.= "po_rel_id = 'system', ";
						$point_sql.= "po_rel_action = '".date("Y-m-d")."' ";

						$point_rst = mysql_query($point_sql);

						if($point_rst)
						{
							$member_sql = "update g4_member set mb_point = mb_point - ".$order_row[pay_point]." where mb_id = '".$order_row[mb_id]."' limit 1";
							$member_rst = mysql_query($member_sql);
						}
					}
				}

				$order_goods_sql = "select * from g4_order_goods where oid = '".$order_row[oid]."' ";
				$order_goods_rst = mysql_query($order_goods_sql);
				while($order_goods_row = mysql_fetch_array($order_goods_rst))
				{
					$goods_update_sql = "update g4_goods set g_cnt = g_cnt - ".$order_goods_row[cnt].", g_sale = g_sale + ".$order_goods_row[cnt]." where g_code='".$order_goods_row[g_code]."' limit 1";

					$goods_update_rst = mysql_query($goods_update_sql);
				}
			}

			$tmp_status_history[] = $list_status."|".date("Y-m-d H:i:s");
			$status_history = implode("/",$tmp_status_history);

			$sql  = "update g4_order set status='".$list_status."', status_history='".$status_history."' where oid='".$order_row[oid]."' limit 1";
		}

		$rst = mysql_query($sql);
	}
}
elseif($Mode == "DEL")
{
	auth_check($auth[$sub_menu], "d");
	$order_row = mysql_fetch_array(mysql_query("select * from g4_order where idx='".$IDX."' limit 1"));

	if($order_row[mb_part]=="member")
	{
		if($order_row[pay_point] > 0)
		{
			$point_sql = "insert into g4_point set ";
			$point_sql.= "mb_id = '".$order_row[mb_id]."', ";
			$point_sql.= "po_datetime = now(), ";
			$point_sql.= "po_content = '주문번호 [".$order_row[oid]."]건 취소', ";
			$point_sql.= "po_point = '".$order_row[pay_point]."', ";
			$point_sql.= "po_rel_table = '@shop', ";
			$point_sql.= "po_rel_id = 'system', ";
			$point_sql.= "po_rel_action = '".date("Y-m-d")."' ";

			$point_rst = mysql_query($point_sql);

			if($point_rst)
			{
				$member_sql = "update g4_member set mb_point = mb_point + ".$order_row[pay_point]." where mb_id = '".$order_row[mb_id]."' limit 1";
				$member_rst = mysql_query($member_sql);
			}
		}
	}

	$order_goods_sql = "select * from g4_order_goods where oid = '".$order_row[oid]."' ";
	$order_goods_rst = mysql_query($order_goods_sql);
	while($order_goods_row = mysql_fetch_array($order_goods_rst))
	{
		$goods_update_sql = "update g4_goods set g_cnt = g_cnt + ".$order_goods_row[cnt].", g_sale = g_sale - ".$order_goods_row[cnt]." where g_code='".$order_goods_row[g_code]."' limit 1";

		$goods_update_rst = mysql_query($goods_update_sql);

		if($goods_update_rst)
		{
			@mysql_query("delete from g4_order_goods where idx='".$order_goods_row[idx]."' limit 1");
		}
	}

	@mysql_query("delete from g4_order where idx='".$order_row[idx]."' limit 1");
}
echo("<script>location.href='./order_list.php'</script>");
?>